package com.best.onegous.details;


import org.json.JSONObject;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.best.onegous.BaseActivity;
import com.best.onegous.R;
import com.best.onegous.agreement.ApiInterface;
import com.best.onegous.http.BusinessResponse;
import com.best.onegous.http.HttpClients;
import com.loopj.android.http.RequestParams;

public class Register extends BaseActivity implements OnClickListener,BusinessResponse{
	private Button sendmsg;
	private EditText iphone,yanzhengma,pass,passs;
	private HttpClients httpclient;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setThem(R.layout.register_activity);
		findViewById(R.id.back).setOnClickListener(this);
		sendmsg=(Button)findViewById(R.id.sendmsg);
        sendmsg.setOnClickListener(this);
        findViewById(R.id.zhuce).setOnClickListener(this);
        iphone = (EditText) findViewById(R.id.iphone);
        yanzhengma = (EditText) findViewById(R.id.yanzhengma);
        pass = (EditText) findViewById(R.id.pass);
        passs = (EditText) findViewById(R.id.passs);
        httpclient = new HttpClients(this);
		httpclient.addResponseListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.zhuce:
			String iphonestr = iphone.getText().toString().trim();
			String yanzhengmastr = yanzhengma.getText().toString().trim();
			String passstr = pass.getText().toString().trim();
			String passsstr = passs.getText().toString().trim();
			if(!"".equals(iphonestr)){
			if(!"".equals(yanzhengmastr)){
				if(!"".equals(passstr)){
					if(passsstr.equals(passstr)){
						  RequestParams params = new RequestParams();
						  params.add("name",iphonestr);
						  params.add("mobile_code",yanzhengmastr);
						  params.add("userpassword",passstr);
						  params.add("userpassword2",passsstr);
						httpclient.httpclient(ApiInterface.ZHUCE, params, true);
					}else{
						toast("两次密码不一致！");
					}
				}else{
					toast("密码不能为空！");
				}
			}else{
				toast("验证码不能为空！");
			}
			}else{
				toast("请填写正确的手机号！");
			}
			
			break;
		case R.id.back:
			 finish();
			break;
		case R.id.sendmsg:
			//发送验证码
			 String phone=iphone.getText().toString().trim();
			 if("".equals(phone)||null==phone||phone.length()<11){
				 	toast("请填写正确的手机号！");
					
					return;
				}
			 
			 //检查该手机号是否已经注册
			 /*RequestParams params = new RequestParams();
				params.add("phone", phone);
		        httpclient.httpclient(ApiInterface.DUANXIN,params);*/
		        updateStatus();
		        //sendmsg.setEnabled(true);
			break;
	
		}
	}
	public void updateStatus() {
		iphone.setEnabled(false);
		//更改发送按钮的状态
		sendmsg.setEnabled(false);
		//sendmsg.setBackgroundColor(Color.parseColor("#c6c5c4"));
		//sendmsg.setTextColor(Color.parseColor("#808080"));
		CountDownTimer countDownTimer=new CountDownTimer(60*1000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
				sendmsg.setText(millisUntilFinished/1000+"秒");
			}
			@Override
			public void onFinish() {
				sendmsg.setText("重新获取");
				sendmsg.setEnabled(true);
				//sendmsg.setBackgroundColor(Color.WHITE);
				//sendmsg.setTextColor(getResources().getColor(R.color.them));
			}
		};
		countDownTimer.start();
	}
	@Override
	public void OnMessageResponse(String url, JSONObject jo){

		if (null != jo) {
			if (url.equals(ApiInterface.ZHUCE)) {
				toast("注册成功！");
				finish();
			}
		}
	}
}
